﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using System.Security.Cryptography;
using System.Net;

namespace Common
{
    public class RegEditor
    {
        // 初始化注册表
        public static void InitReg()
        {
            SetClientStatusToRegister(ClientStatus.None);
        }

        private static string GetMD5_32(string s, string _input_charset)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] t = md5.ComputeHash(Encoding.GetEncoding(_input_charset).GetBytes(s));
            StringBuilder sb = new StringBuilder(32);
            for (int i = 0; i < t.Length; i++)
            {
                sb.Append(t[i].ToString("x").PadLeft(2, '0'));
            }

            return sb.ToString();
        }

        private const string KEYNAME_MAIN = "mainFlag";
        private const string KEYNAME_LOCKER = "lockerFlag";
        public static void SetClientStatusToRegister(ClientStatus pStatus)
        {
            switch (pStatus)
            { 
                case ClientStatus.LockerScreenActive:
                    RegEdit(KEYNAME_LOCKER, GetMD5_32(Dns.GetHostName(), "UTF-8"));
                    break;
                case ClientStatus.LockerScreenNone:
                    RegEdit(KEYNAME_LOCKER, "0");
                    break;
                case ClientStatus.None:
                    RegEdit(KEYNAME_LOCKER, "0");
                    RegEdit(KEYNAME_MAIN, "0");
                    break;
                case ClientStatus.MainScreenActive:
                    RegEdit(KEYNAME_MAIN, "1");
                    break;
                case ClientStatus.MainScreenNone:
                    RegEdit(KEYNAME_MAIN, "0");
                    break;
            }
        }

        private static void RegEdit(string pKey, string pValue)
        {
            RegistryKey FCkey = GetFCKey();
            FCkey.SetValue(pKey, pValue);
            FCkey.Close();
        }

        private static RegistryKey GetFCKey()
        {
            RegistryKey softwareKey = Registry.CurrentUser.OpenSubKey("software", true);
            RegistryKey FCkey = null;
            if (!softwareKey.GetSubKeyNames().Contains("FCService"))
            {
                FCkey = softwareKey.CreateSubKey("FCService");
            }
            else
            {
                FCkey = softwareKey.OpenSubKey("FCService", true);
            }
            return FCkey;
        }

        /// <summary>
        /// 获取主窗体对应的注册表键值
        /// </summary>
        /// <returns></returns>
        public static string GetMainFlagValue()
        {
            return GetKeyValue(KEYNAME_MAIN);
        }

        /// <summary>
        /// 获取锁屏对应注册表的键值
        /// </summary>
        /// <returns></returns>
        public static string GetLockerFlagValue()
        {
            return GetKeyValue(KEYNAME_LOCKER);
        }

        /// <summary>
        /// 根据Key获取Value
        /// </summary>
        /// <param name="pKey"></param>
        /// <returns></returns>
        private static string GetKeyValue(string pKey)
        {
            RegistryKey FCkey = GetFCKey();
            return FCkey.GetValue(pKey).ToString();   
        }

        //public static bool IsLockerActive()
        //{
        //    return GetLockerFlagValue() == "1";
        //}
    }

    /// <summary>
    /// 当前客户端状态
    /// </summary>
    public enum ClientStatus
    {
        
        None, //主页面和锁屏页面都关闭状体
        MainScreenNone, // 主页面关闭状态
        MainScreenActive, // 主页面启动状态
        LockerScreenNone, // 锁屏页面关闭状态
        LockerScreenActive // 锁屏页面启动状态
    }
}
